#include "showwidget.h"
#include <QApplication>
#include <QMenu>
#include <QMessageBox>
#include "editwidget.h"
#include "newdialog.h"
#include "QAction"


ShowWidget::ShowWidget(QScrollArea *parent)
    : QScrollArea(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setMinimumSize((Screen::GetScreenWidth())>>4, (Screen::GetScreenHeight())>>4);
    setMaximumSize(Screen::GetScreenSize());
    setFrameShape(QScrollArea::NoFrame);
    setWidgetResizable(true);

    m_curr_xml_name = "";
}

ShowWidget::~ShowWidget()
{

}

EditWidget *ShowWidget::getEditWidget()
{
    EditWidget *edit = qobject_cast<EditWidget *>(widget());
    if (NULL == edit) return NULL;
    return edit;
}

QString ShowWidget::CreateItem(ObjectInfo param,  QPoint pos, QString &struct_show_item_name)
{
    QPoint cur_pos;
    cur_pos = mapFromGlobal(pos);

    if (cur_pos.x() >= size().width() || cur_pos.y() >= size().height())
        return "";

    EditWidget *edit = dynamic_cast<EditWidget *>(widget());
    if (NULL == edit)
       return "";

    cur_pos = edit->mapFromGlobal(pos);
    if (cur_pos.y() >= edit->height())
        return "";

    QString random = edit->AutoGenerate(param.object_name, struct_show_item_name, myapp::to_mid(cur_pos, param.width, param.height), 100, 100);

    edit->show_component();

    //确保添加之后即选中,但是此时右上角控件还未生成所以不能选中
    edit->slt_find_changed_viewobject(random);

    return random;
}

void ShowWidget::savecfg()
{
    EditWidget *edit = dynamic_cast<EditWidget *>(widget());
    if (edit) edit->savecfg();
}

void ShowWidget::slt_show_widget_by_file(QString xml_name)
{
    QWidget *subwidget = takeWidget();
    if (subwidget)
    {
        EditWidget *edit = qobject_cast<EditWidget*>(subwidget);
        disconnect(edit, &EditWidget::sgl_attribute_to_showwidget, this, &ShowWidget::slt_select_and_show);
        disconnect(edit, &EditWidget::sgl_update_to_showwidget, this, &ShowWidget::slt_only_show);
        disconnect(this, &ShowWidget::signal_list_to_graphics, edit, &EditWidget::slt_list_to_graphics);
        disconnect(edit, &EditWidget::signal_delete, this, &ShowWidget::signal_delete);
        disconnect(edit, &EditWidget::signalCopy, this, &ShowWidget::signal_copy);
        edit->setParent(NULL);
        edit->setVisible(false);
        //to do something
    }
    m_curr_xml_name = xml_name;
    EditWidget *edit_widget = qobject_cast<EditWidget*>(myapp::mDynamicMapParam->mMapObjectShow[xml_name]);
    connect(edit_widget, &EditWidget::sgl_attribute_to_showwidget, this, &ShowWidget::slt_select_and_show);
    connect(edit_widget, &EditWidget::sgl_update_to_showwidget, this, &ShowWidget::slt_only_show);
    connect(this, &ShowWidget::signal_list_to_graphics, edit_widget, &EditWidget::slt_list_to_graphics);
    connect(edit_widget, &EditWidget::signal_delete, this, &ShowWidget::signal_delete);
    connect(edit_widget, &EditWidget::signalCopy, this, &ShowWidget::signal_copy);
    edit_widget->setParent(this);
    edit_widget->setVisible(true);
    edit_widget->show_component();
    setWidget(edit_widget);
}

void ShowWidget::refresh_show_widget()
{
    if (m_curr_xml_name != "")
        slt_show_widget_by_file(m_curr_xml_name);
}

void ShowWidget::add_right_click_action_refresh()
{
    QAction *refresh_action = new QAction(tr("刷新"), this);
    addAction(refresh_action);
    setContextMenuPolicy(Qt::ActionsContextMenu);
    connect(refresh_action, SIGNAL(triggered()), this, SLOT(refresh_show_widget()));
}

void ShowWidget::slt_select_and_show(QString id, ViewObjectInfo* info)
{
    if ((id != "") && (NULL != info))
    {
        emit sgl_to_structurewidget_to_select(id, info->object->GetObjectParam()->GetName());
        emit sgl_to_attributewidget_to_show(id, info);
    }
}

void ShowWidget::slt_only_show(QString id, ViewObjectInfo* info)
{
    if ((id != "") && (NULL != info))
    {
        emit sgl_to_attributewidget_to_show(id, info);
    }
}


